<?php
namespace App\Services;
use Exception;

class CommonService
{
    public static function getSHortLinksId($short_id)
    {
        $index = -1;
        $len = strlen($short_id);
        for ($i = 0; $i < $len; $i++) {
            $char = substr($short_id, $i, 1);
            $isCh = preg_match("/^[" . chr(0xa1) . "-" . chr(0xff) . "]+$/", $char); // 判断是否是中文
            if ($isCh) {
                $index = $i;
                break;
            }
        }
        $short_id = substr($short_id,0,$index);
        return $short_id;
    }
    
    public static function getRandomCode()
    {
        $dictionaries = [["A","B","a","b","0"],["C","D","c","d","1"],["E","F","e","f","2"],["G","H","g","h","3"],["I","J","i","j","4"],["K","L","k","l","5"],["M","N","m","n","6"],["O","P","o","p","7"],["Q","R","q","r","8"],["S","T","s","t","9"]];
        $times = str_split(time());
        $code = "";
        foreach ($times as $time) {
            $dictionarie = $dictionaries[$time];
            $code .= $dictionarie[array_rand($dictionarie,1)];
        }
        return $code;
    }

    public static function getRandomCodes($codes)
    {
        $dictionaries = [["A","B","a","b","0"],["C","D","c","d","1"],["E","F","e","f","2"],["G","H","g","h","3"],["I","J","i","j","4"],["K","L","k","l","5"],["M","N","m","n","6"],["O","P","o","p","7"],["Q","R","q","r","8"],["S","T","s","t","9"]];
        $times = str_split($codes);
        $code = "";
        foreach ($times as $time) {
            $dictionarie = $dictionaries[$time];
            $code .= $dictionarie[array_rand($dictionarie,1)];
        }
        return $code;
    }

    public static function b64dec($b64) {
        // 64进制转换成10进制
        $dict = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-';
        $dec = 0;
        $len = strlen($b64);
        for ($i = 0; $i < $len; $i++) {
            $b = strpos($dict, $b64[$i]);
            $j = $len - $i - 1;
            $dec += ($j == 0 ? $b : (2 << (6 * $j - 1)) * $b);
        }
        return $dec;
    }

    public static function decb64($dec) {
        // 10进制转换成64进制，最长5位
        $dict = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        $b62 = '';
        do {
            $b62 = $dict[($dec % 62)] . $b62;
            $dec /= 62;
        } while ($dec >= 1);
        return $b62;
    }

    public static function aesEncryptone($data)
    {
        $key = 'Cd2sd56Msd789fsd';
        $iv = 'AfMd456Msd789fDC';
        $encrypted = base64_encode(openssl_encrypt($data,"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv));
        return $encrypted;
    }

    public static function aesDncryptone($data)
    {
        $key = 'Cd2sd56Msd789fsd';
        $iv = 'AfMd456Msd789fDC';
        $decrypted = openssl_decrypt(base64_decode($data), 'AES-128-CBC', $key,OPENSSL_RAW_DATA,$iv);
        return $decrypted;
    }

    public static function md5Encrypt($data)
    {
        $sign = md5("Bo".$data."Komd");
        return $sign;
    }

    /**
     * 获取指定日期段内每一天的日期
     * @param  Date  $startdate 开始日期
     * @param  Date  $enddate   结束日期
     * @return Array
     */
    public static function getDateFromRange($startdate, $enddate,$format){
 
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);
 
        // 计算日期段内有多少天
        $days = ($etimestamp-$stimestamp)/86400;
 
        // 保存每天日期
        $date = array();
 
        for($i=0; $i<$days; $i++){
            $date[] = date($format, $stimestamp+(86400*$i));
        }
        return $date;
    }
}
